package com.arron.algorithm.leetcodetop100.二分查找;

import java.util.HashSet;
import java.util.Set;

/**
 *  原地hash法
 * @see com.arron.algorithm.leetcodetop100.技巧.原地hash.丢失的数字
 *  异或法：
 * @see com.arron.algorithm.leetcodetop100.技巧.异或.丢失的数字
 */
public class 丢失的数字 {



    // 二分查找
    public int missingNumber_plus(int[] nums) {

        int left = 0;
        int right = nums.length;

        while (left<right){
            int cnt = 0;
            int mid = left + (right-left)/2;

            for (int i = 0; i < nums.length; i++) {
                if (nums[i] <= mid){
                    cnt++;
                }
            }

            if (cnt < mid+1){
                right = mid;
            }else {
                left = mid+1;
            }
        }
        return left;

    }


    public int missingNumber(int[] nums) {

        Set<Integer> set = new HashSet<>();
        for (int i = 0; i <nums.length; i++) {
            set.add(nums[i]);
        }
        for (int i = 0; i <=nums.length ; i++) {
            if (!set.contains(i)){
                return i;
            }
        }
        return -1;
    }

}
